Add cache_response option to completable form fields

When `cache_response` is set to `false` for completable fields the returned options will not be
cached on the client side. The default is `true`.

Clearing the cache is needed when the completable response depends on a different option of the Agent.

Usage Example:

```
form_configurable :models, roles: :completable, cache_response: false
```

* Disable the completion callback when `select2` is opened programmatically to avoid an infinite loop
* Moved the completion ajax call from `select2-open` to `select2-opening` to clear the cached results before
 `completableDefaultOptions` is called (shows the "Loading ..." message instead of the previous results)

Dominik Sander 7 years ago
parent
commit
3e76fa0e41

+ 7 - 2
app/assets/javascripts/components/form_configurable.js.coffee

@@ -53,10 +53,13 @@ $ ->
53 53
     updateDropdownData = (form_data, element, data) ->
54 54
       returnedResults[form_data.attribute] = {text: 'Options', children: data}
55 55
       $(element).trigger('change')
56
+      $("input[role~=completable]").off 'select2-opening', select2OpeningCallback
56 57
       $(element).select2('open')
58
+      $("input[role~=completable]").on 'select2-opening', select2OpeningCallback
57 59
 
58
-    $("input[role~=completable]").on 'select2-open', (e) ->
60
+    select2OpeningCallback = (e) ->
59 61
       form_data = getFormData(e.currentTarget)
62
+      delete returnedResults[form_data.attribute] if returnedResults[form_data.attribute] && !$(e.currentTarget).data('cacheResponse')
60 63
       return if returnedResults[form_data.attribute]
61 64
 
62 65
       $.ajax '/agents/complete',
@@ -67,10 +70,12 @@ $ ->
67 70
         error: (data) ->
68 71
           updateDropdownData(form_data, e.currentTarget, [{id: undefined, text: 'Error loading data.'}])
69 72
 
73
+    $("input[role~=completable]").on 'select2-opening', select2OpeningCallback
74
+
70 75
     $("input[type=radio][role~=form-configurable]").change (e) ->
71 76
       input = $(e.currentTarget).parents().siblings("input[data-attribute=#{$(e.currentTarget).data('attribute')}]")
72 77
       if $(e.currentTarget).val() == 'manual'
73 78
         input.removeClass('hidden')
74 79
       else
75 80
         input.val($(e.currentTarget).val())
76
-        input.addClass('hidden')
81
+        input.addClass('hidden')

+ 1 - 1
app/concerns/form_configurable.rb

@@ -32,7 +32,7 @@ module FormConfigurable
32 32
       options = args.extract_options!.reverse_merge(roles: [], type: :string)
33 33
 
34 34
       if args.all? { |arg| arg.is_a?(Symbol) }
35
-        options.assert_valid_keys([:type, :roles, :values, :ace])
35
+        options.assert_valid_keys([:type, :roles, :values, :ace, :cache_response])
36 36
       end
37 37
 
38 38
       if options[:type] == :array && (options[:values].blank? || !options[:values].is_a?(Array))

+ 2 - 2
app/presenters/form_configurable_agent_presenter.rb

@@ -43,7 +43,7 @@ class FormConfigurableAgentPresenter < Decorator
43 43
         @view.concat(@view.text_field_tag "agent[options][#{attribute}]", value, html_options.merge(:class => "form-control #{@agent.send(:boolify, value) != nil ? 'hidden' : ''}"))
44 44
       end
45 45
     when :array, :string
46
-      @view.text_field_tag "agent[options][#{attribute}]", value, html_options.merge(:class => 'form-control')
46
+      @view.text_field_tag "agent[options][#{attribute}]", value, html_options.deep_merge(:class => 'form-control', data: {cache_response: data[:cache_response] != false})
47 47
     end
48 48
   end
49
-end
49
+end